﻿<DockPanel   x:Class="DevControlLibrary.UI.DBTreeViewControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             xmlns:LocalTemplate="clr-namespace:DevControlLibrary.UI"
             d:DesignHeight="300" d:DesignWidth="300"
             >
    
    <DockPanel.Resources>
        <LocalTemplate:ContextMenuDataTemplateSelector x:Key="MenuDataTemplateSelector"/>
        <LocalTemplate:IsNullConverter x:Key="NullConverter"/>
        <!--地址模版-->
        <ContextMenu x:Key="ServerMenu">
            <MenuItem Header="移除数据库" 
                    Command="{Binding PlacementTarget.Tag.DeleteCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                    CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}" >
                <MenuItem.Icon>
                    <Image Source="/QueryResource;component/Images/Tool/Delete_16x16.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
        </ContextMenu>
        <HierarchicalDataTemplate x:Key="ServerTemplate" ItemsSource="{Binding ChildNodes}">
            <StackPanel  Orientation="Horizontal" ContextMenu="{StaticResource ServerMenu}" Tag="{Binding DataContext, ElementName=dbtreeview}">
                <Image Source="{Binding ImageUrl}" Height="16" Width="16"/>
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </HierarchicalDataTemplate>
        
        <!--描述模版-->
        <ContextMenu x:Key="DescriptionMenu">
            <MenuItem Header="转换"
                      Command="{Binding PlacementTarget.Tag.TransFormCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/DevControlLibrary;component/Images/WordWrap.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
        </ContextMenu>
        <HierarchicalDataTemplate x:Key="DescriptionTemplate" ItemsSource="{Binding ChildNodes}">
            <StackPanel  Orientation="Horizontal" ContextMenu="{StaticResource DescriptionMenu}" Tag="{Binding DataContext,ElementName=dbtreeview}">
                <Image Source="{Binding ImageUrl}" Height="16" Width="16"/>
                <TextBlock Text="{Binding Name}"/>
                <TextBlock Text="{Binding Count,StringFormat=({0})}" />
            </StackPanel >
        </HierarchicalDataTemplate>

        <!--数据库模版-->
        <ContextMenu x:Key="DBMenu">
            <MenuItem Header="连接数据库"
                     Command="{Binding PlacementTarget.Tag.ConnectDBCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                     CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}"  >
                <MenuItem.Icon>
                    <Image Source="/DevControlLibrary;component/Images/btnConnect.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="断开数据库" 
                    Command="{Binding PlacementTarget.Tag.UnConnectDBCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                    CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}" >
                <MenuItem.Icon>
                    <Image Source="/DevControlLibrary;component/Images/btnUnConnect.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <Separator/>
            <MenuItem Header="刷新" 
                    Command="{Binding PlacementTarget.Tag.RefreshCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                    CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}" >
                <MenuItem.Icon>
                    <Image Source="/DevControlLibrary;component/Images/WordWrap.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <Separator/>
            <MenuItem Header="移除数据库" 
                    Command="{Binding PlacementTarget.Tag.DeleteCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                    CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}" >
                <MenuItem.Icon>
                    <Image Source="/QueryResource;component/Images/Tool/Delete_16x16.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
        </ContextMenu>
        <HierarchicalDataTemplate x:Key="DBTemplate" ItemsSource="{Binding ChildNodes}" >
            <StackPanel  Orientation="Horizontal" ContextMenu="{StaticResource DBMenu}" Tag="{Binding DataContext, ElementName=dbtreeview}">
                <Image Source="{Binding ImageUrl}" Height="16" Width="16"/>
                <TextBlock Text="{Binding Name}"/>
                <TextBlock Text="{Binding ConnectionSource.ConnectType,StringFormat=[{0}]}"/>
            </StackPanel >
        </HierarchicalDataTemplate>

        <!--表模版-->
        <ContextMenu x:Key="TableMenu">
            <MenuItem Header="查询所有记录(_S)"
                      Command="{Binding PlacementTarget.Tag.SelectAllCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/DevControlLibrary;component/Images/WordWrap.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="查询前100"
                      Command="{Binding PlacementTarget.Tag.SelectTopCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/DevControlLibrary;component/Images/WordWrap.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="查询所有记录并执行"
                      Command="{Binding PlacementTarget.Tag.SelectRunCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/DevControlLibrary;component/Images/WordWrap.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <Separator/>
            <MenuItem Header="导出表结构"
                      Command="{Binding PlacementTarget.Tag.ExprotTableStructureCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/DevControlLibrary;component/Images/WordWrap.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <Separator/>
            <MenuItem Header="转换"
                      Command="{Binding PlacementTarget.Tag.TransFormCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/DevControlLibrary;component/Images/WordWrap.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="复制(_C)"
                      Command="{Binding PlacementTarget.Tag.CopyCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/QueryResource;component/Images/Tool/copy-16x16.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <Separator/>
            <MenuItem Header="表定义"
                      Command="{Binding PlacementTarget.Tag.TableDefinitionCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/DevControlLibrary;component/Images/WordWrap.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="添加描述"
                      Command="{Binding PlacementTarget.Tag.AddDescriptionCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/DevControlLibrary;component/Images/WordWrap.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
        </ContextMenu>
        <HierarchicalDataTemplate x:Key="TableTemplate" ItemsSource="{Binding ChildNodes}" >
            <StackPanel  Orientation="Horizontal" ContextMenu="{StaticResource TableMenu}" Tag="{Binding DataContext,ElementName=dbtreeview}">
                <Image Source="{Binding ImageUrl}" Height="16" Width="16"/>
                <TextBlock Text="{Binding Name}"/>
                <StackPanel.ToolTip>
                    <TextBlock Text="{Binding ChName}"/>
                </StackPanel.ToolTip>
            </StackPanel >
        </HierarchicalDataTemplate>

        <!--表描述模版-->
        <HierarchicalDataTemplate x:Key="TableInfoTemplate" ItemsSource="{Binding ChildNodes}">
            <StackPanel  Orientation="Horizontal" Tag="{Binding DataContext, ElementName=dbtreeview}">
                <Image Source="{Binding ImageUrl}" Height="16" Width="16"/>
                <TextBlock Text="{Binding Name}"/>
                <TextBlock Text="{Binding Count,StringFormat=({0})}" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <!--列模版-->
        <ContextMenu x:Key="FieldMenu">
            <MenuItem Header="复制(_C)" 
                      Command="{Binding PlacementTarget.Tag.CopyCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/QueryResource;component/Images/Tool/copy-16x16.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="添加列"
                      Command="{Binding PlacementTarget.Tag.AddColumnInfoCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/DevControlLibrary;component/Images/WordWrap.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
        </ContextMenu>
        <HierarchicalDataTemplate x:Key="FieldTemplate" ItemsSource="{Binding ChildNodes}">
            <StackPanel  Orientation="Horizontal" ContextMenu="{StaticResource FieldMenu}" Tag="{Binding DataContext,ElementName=dbtreeview}">
                <Image Source="{Binding ImageUrl}" Height="16" Width="16"/>
                <TextBlock Text="{Binding Name}"/>
                <TextBlock Text=" ("/>
                <TextBlock Text="{Binding TypeName}"/>
                <TextBlock Text="{Binding LengthString,StringFormat=[{0}]}"/>
                <TextBlock Text="{Binding IsNull,Converter={StaticResource NullConverter}}"/>
                <TextBlock Text=")"/>
                <StackPanel.ToolTip>
                    <TextBlock Text="{Binding ChName}"/>
                </StackPanel.ToolTip>
            </StackPanel>
        </HierarchicalDataTemplate>

        <!--外键模版-->
        <HierarchicalDataTemplate x:Key="ForeignTemplate" ItemsSource="{Binding ChildNodes}">
            <StackPanel  Orientation="Horizontal" ContextMenu="{StaticResource FieldMenu}" Tag="{Binding DataContext,ElementName=dbtreeview}">
                <Image Source="{Binding ImageUrl}" Height="16" Width="16"/>
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </HierarchicalDataTemplate>
        <!--索引模版-->
        <HierarchicalDataTemplate x:Key="IndexTemplate" ItemsSource="{Binding ChildNodes}">
            <StackPanel  Orientation="Horizontal" Tag="{Binding DataContext,ElementName=dbtreeview}">
                <Image Source="{Binding ImageUrl}" Height="16" Width="16"/>
                <TextBlock Text="{Binding Name}"/>
                <TextBlock Text="{Binding Sequence,StringFormat=({0})}" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <!--索引描述模版-->
        <HierarchicalDataTemplate x:Key="IndexDescriptionTemplate" ItemsSource="{Binding ChildNodes}">
            <StackPanel  Orientation="Horizontal" ContextMenu="{StaticResource FieldMenu}" Tag="{Binding DataContext,ElementName=dbtreeview}">
                <Image Source="{Binding ImageUrl}" Height="16" Width="16"/>
                <TextBlock Text="{Binding Name}"/>
                <TextBlock Text="{Binding UniqueString}" />
            </StackPanel>
        </HierarchicalDataTemplate>
        
        <!--视图模版-->
        <ContextMenu x:Key="ViewMenu">
            <MenuItem Header="打开(_O)"
                      Command="{Binding PlacementTarget.Tag.ShowNewWindowCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/QueryResource;component/Images/Tool/open-16x16.png" Height="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="复制(_C)" 
                      Command="{Binding PlacementTarget.Tag.CopyCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/QueryResource;component/Images/Tool/copy-16x16.png" Height="16"/>
                </MenuItem.Icon>
            </MenuItem>
        </ContextMenu>
        <HierarchicalDataTemplate x:Key="ViewTemplate" ItemsSource="{Binding ChildNodes}" >
            <StackPanel  Orientation="Horizontal" ContextMenu="{StaticResource ViewMenu}" Tag="{Binding DataContext,ElementName=dbtreeview}">
                <Image Source="{Binding ImageUrl}" Height="16" Width="16"/>
                <TextBlock Text="{Binding Name}"/>
                <TextBlock Text="{Binding Count,StringFormat=[{0}]}"/>
            </StackPanel>
        </HierarchicalDataTemplate>

        <!--存储过程模版-->
        <ContextMenu x:Key="ProcMenu">
            <MenuItem Header="打开(_O)"
                      Command="{Binding PlacementTarget.Tag.ShowNewWindowCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/QueryResource;component/Images/Tool/open-16x16.png" Height="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="复制(_C)" 
                      Command="{Binding PlacementTarget.Tag.CopyCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/QueryResource;component/Images/Tool/copy-16x16.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="执行(_E)" 
                      Command="{Binding PlacementTarget.Tag.ShowProcInfoCommand,RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="/QueryResource;component/Images/Tool/AppointmentExport.png" Height="16" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
        </ContextMenu>
        <HierarchicalDataTemplate x:Key="ProcTemplate" ItemsSource="{Binding ChildNodes}" >
            <StackPanel  Orientation="Horizontal" ContextMenu="{StaticResource ProcMenu}" Tag="{Binding DataContext,ElementName=dbtreeview}">
                <Image Source="{Binding ImageUrl}" Height="16"/>
                <TextBlock Text="{Binding Name}"/>
                <TextBlock Text="{Binding Count,StringFormat=[{0}]}"/>
            </StackPanel >
        </HierarchicalDataTemplate>
        
        <!--存储过程参数模版-->
        <HierarchicalDataTemplate x:Key="ProcParamTemplate" ItemsSource="{Binding ChildNodes}" >
            <StackPanel  Orientation="Horizontal" Tag="{Binding DataContext,ElementName=dbtreeview}">
                <Image Source="{Binding ImageUrl}" Height="16"/>
                <TextBlock Text="{Binding Name}"/>
                <TextBlock Text=" ("/>
                <TextBlock Text="{Binding TypeName}"/>
                <TextBlock Text="{Binding LengthString,StringFormat=[{0}]}"/>
                <TextBlock Text="{Binding Status,StringFormat=-{0}}"/>
                <TextBlock Text=")"/>
            </StackPanel >
        </HierarchicalDataTemplate>
    </DockPanel.Resources>
    <Grid>   
        <TreeView Name="dbtreeview"    
                  
                  ItemTemplateSelector ="{StaticResource MenuDataTemplateSelector}" 
                  ItemsSource="{Binding DataSource}" 
                  PreviewMouseRightButtonDown="DbtreeviewPreviewMouseRightButtonDown"
                  SelectedItemChanged="DbtreeviewSelectedItemChanged" >
            <!--虚拟化  但会造成选中的延迟 
            VirtualizingStackPanel.IsVirtualizing="True"
            VirtualizingStackPanel.VirtualizationMode="Recycling"-->
            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded,Mode=TwoWay}"/>
                    <Setter Property="IsSelected" Value="{Binding IsSelected,Mode=TwoWay}"/>
                    <!--<Setter Property="IsFocused" Value="{Binding IsFocused,Mode=TwoWay}" />-->
                    <!--<Style.Triggers>
                        <Trigger Property="IsSelected" Value="False" >
                            <Setter Property="IsExpanded" Value="False"/>
                        </Trigger>
                    </Style.Triggers>-->
                </Style>
            </TreeView.ItemContainerStyle>
        </TreeView>
    </Grid>
</DockPanel>
